package com.inspector.app.ui.my

import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.commen.base.BaseFragment
import com.example.commen.utile.CommonUtils
import com.example.commen.utile.SingleClick
import com.hjq.toast.Toaster
import com.inspector.app.BuildConfig
import com.inspector.app.R
import com.inspector.app.databinding.FragmentAboutBinding
import com.inspector.app.util.ConstantUtil
import com.inspector.app.util.SharedPreferencesUtils

/**
 * @author zlq
 * @date 2019/6/12
 */
class AboutFragment : BaseFragment() {
    private var num = 0 //5次查看地址
    private var clipboard: ClipboardManager? = null
    private lateinit var _binding: FragmentAboutBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentAboutBinding.inflate(inflater, container, false)
        initView()
        initLisenter()
        return _binding.root
    }

    private fun initView() {
        clipboard =
            requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        _binding.commonTitle.commonTitleTV.setText(R.string.about)
        _binding.tvSoftVersionShow.text = CommonUtils.getVersionName(requireActivity())
        val info =
            _binding.tvGenerationDataSoftShow.setText(SharedPreferencesUtils.getString(ConstantUtil.INSTALLDATE))
    }

    @SuppressLint("SetTextI18n")
    private fun initLisenter() {
        SingleClick(_binding.commonTitle.commonTitleBackView) {
            if (fragmentManager != null) {
                fragmentManager?.popBackStack()
            }
        }

        SingleClick(_binding.imgCopys) {
            val clipData = ClipData.newPlainText(null, _binding.tvVx.text.toString()) // 把数据复制到剪贴板
            clipboard!!.setPrimaryClip(clipData)
            Toaster.show("内容已复制到粘贴板！")
        }
        SingleClick(_binding.tvGenerationDataSoft) {
            num++
            if (num == 5) {
                try {
                    _binding.rlInfoName.visibility = View.VISIBLE
                    _binding.tvInfoText.text = "版本详情:"
                    _binding.tvInfoTextShow.text = """
                            ${"版本省份:" + BuildConfig.FLAVOR}
                            版本地址:${BuildConfig.BASE_URL}
                            """.trimIndent()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            } else {
                _binding.rlInfoName.visibility = View.GONE
                _binding.tvInfoText.text = " "
                _binding.tvInfoTextShow.text = " "
            }
            if (num > 5) {
                num = 0
            }
        }

    }


    companion object {
        var flag: Int = 1
    }
}
